// pages/common/recruitment-intro/index.js
import {getDetail, getDeliverList, edit} from "../../../service/Position";
import Config from "../../../main.config";
import {openDocument} from "../../../utils/Wx";
import {getStorageSync} from "../../../utils/Storage";
import {downloadFile} from "../../../service/Common";
import Toast from "@vant/weapp/toast/toast";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        itemInfo: {},
        //投递列表
        deliverList: {
            loading: false,
            reset: false,
            error: false,
            total: -1,
            list: []
        },
        closeBtnLoading: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getRecruitmentDetail(options.id)
    },

    /*
    * 跳转查看详情
    * */
    bindNavigatorToDetail: function () {
        let id = this.data.itemInfo.id;
        wx.navigateTo({
            url: "/pages/common/recruitment-detail/index?id=" + id
        })
    },

    /*
    * 获取招聘详情
    * */
    getRecruitmentDetail: async function (id) {
        try {
            let createUser = getStorageSync(Config.storage.registerInfo + ".createUser");
            let detail = await getDetail({id, createUser});
            let logoUrl = Config.api.downLoad + "?fileId=" + (detail.logo || detail.companyLogo);
            this.setData({
                itemInfo: {
                    id: detail.id,
                    name: detail.name,
                    type: detail.type,
                    salary: detail.salary,
                    tags: detail.tags,
                    logo: logoUrl,
                    publisherName: detail.staffName || detail.companyName,
                    resumeCount: detail.resumeCount || 0,
                    createUser: detail.createUser
                },
                "deliverList.loading": true
            })
        } catch (e) {
            console.error(e)
        }
    },

    /*
    * 关闭职位
    * */
    bindCloseRecruitment: async function () {
        try {
            let id = this.data.itemInfo.id;
            let type = this.data.itemInfo.type;
            let closeBtnLoading = this.data.closeBtnLoading;
            if (closeBtnLoading) return false;
            this.setData({closeBtnLoading: true});
            type = type !== 2 ? 2 : 1;
            await edit({
                id,
                type
            })
            this.setData({"itemInfo.type": type});
        } catch (e) {
            console.error(e)
        } finally {
            this.setData({closeBtnLoading: false});
        }
    },

    /*
    * 列表加载
    * */
    onLoadList: async function (e) {
        try {
            let paging = e.detail.paging;
            let list = this.data.deliverList.list;
            let isReset = this.data.deliverList.reset;
            let positionId = this.data.itemInfo.id;
            let res = await getDeliverList({
                page: paging.page,
                limit: paging.pageSize,
                positionId
            });
            let newList = res.data.map(function (item) {
                let logoUrl = Config.api.downLoad + "?fileId=" + item.logo
                return {
                    createTime: item.createTime,
                    logo: logoUrl,
                    deptName: item.deptName,
                    studentName: item.studentName,
                    staffName: item.staffName,
                    schoolName: item.schoolName,
                    professionalName: item.professionalName,
                    grade: item.grade.split(";")[1].substr(0, 4),
                    resume: item.resume
                }
            });
            list = isReset ? newList : list.concat(newList);
            this.setData({
                "deliverList.list": list,
                "deliverList.total": res.count
            });
        } catch (e) {
            console.error(e)
            this.setData({
                "deliverList.error": true
            })
        } finally {
            this.setData({
                "deliverList.reset": false,
                "deliverList.loading": false
            })
        }
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.setData({"collect.loading": true});
    },

    /*
    * 查看简历
    * */
    bindShowResume: async function (e) {
        try {
            let index = e.currentTarget.dataset.index;
            let list = this.data.deliverList.list;
            let currentResume = list[index].resume;
            Toast.loading({
                message: "下载中...",
                duration: 0,
                context: this
            });
            let tempFilePath = await downloadFile(currentResume);
            Toast.clear();
            Toast.loading({
                message: "打开中...",
                duration: 0,
                context: this,

            });
            await openDocument(tempFilePath);
            Toast.clear();
        } catch (e) {
            Toast({
                type: "fail",
                message: "打开失败！",
                context: this
            })
            console.error(e)
        }
    }

})